<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Lansexiongdi extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $type = 0;
        $account = $bean->getAccount();
        // 判断账号
        if (preg_match("/^1[34578]\d{9}$/", $account)) {
            $type = 1;
        } elseif (preg_match("/^[1-9]\d{4,10}$/", $account)) {
            $type = 2;
        }
        $array = [
            'merchantId'        => $bean->getSupplierNum(), //商户号
            'outTradeNo'        => $bean->getOrderNo(), //订单号
            'productId'         => $bean->getGoodsId(), //商品编码
            'rechargeAccount'   => $account,
            'accountType'       => $type,
            'number'            => $bean->getRechargeNum() * $bean->getStartCharging(),//充值数量
            'timeStamp'         => time(),
            'notifyUrl'         => $bean->getNotifyUrl(), //异步回调地址
        ];
        ksort($array);
        $sign='';
        foreach ($array as $key => $value){
            $sign.=sprintf('%s=%s&', $key, $value);
        }
        $array['sign'] = strtoupper(md5($sign.'key='. $bean->getSecretKey()));
        $array['version'] = 1.0;
        $array['extendParameter'] = [];

        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('status', '');
        $order_sn = $request->input('outTradeNo', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['1', '3']))->setRechargeSuccess($status == '1');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response('success');
    }
}
